package frontend.parser.syntaxtree;

import frontend.lexer.TKType;
import frontend.parser.syntaxtree.symbol.NonTerminalSymbol;
import frontend.parser.syntaxtree.symbol.TerminalSymbol;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

public class InitValNodeForArray extends InitValNode {
    public List<ExpNode> initVals = new ArrayList<>();

    @Override
    public void walk(Consumer<TerminalSymbol> terConsumer, Consumer<NonTerminalSymbol> nonTerConsumer) {
        terConsumer.accept(new TerminalSymbol(TKType.LBRACE));

//        boolean first = true;
//        for (var init : initVals) {
//            if (first) {
//                first = false;
//            } else {
//                terConsumer.accept(new TerminalSymbol(TKType.COMMA));
//            }
//            init.walk(terConsumer, nonTerConsumer);
//        }
        Iterator<ExpNode> iterator = initVals.iterator();
        if (iterator.hasNext()) {//对于第一个
            iterator.next().walk(terConsumer, nonTerConsumer);
        }
        iterator.forEachRemaining(constDef -> {//对于剩下的
            terConsumer.accept(new TerminalSymbol(TKType.COMMA));
            constDef.walk(terConsumer, nonTerConsumer);
        });
        terConsumer.accept(new TerminalSymbol(TKType.RBRACE));

        nonTerConsumer.accept(new NonTerminalSymbol(this));
    }
}
